package com.sdut.material.controller;


import com.sdut.material.pojo.User;
import com.sdut.material.service.IUserService;
import com.sdut.material.util.JSONResult;
import com.sdut.material.util.LayUITableResult;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Yu
 * @since 2022-04-20
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;
    @RequestMapping("/getUserListPage")
    public String getUserListPage() {
        return "user/user_list";
    }
    @RequestMapping("/selectByPage")
    @ResponseBody
    public LayUITableResult selectByPage(Long page,Long limit) {
        return userService.selectByPage(page,limit);
    }
    @RequestMapping("/getUserInsertPage")
    public String getUserInsertPage() {
        return "user/user_insert";
    }
    @RequestMapping("/insert")
    @ResponseBody
    public JSONResult insert(User user,String roles) {
        userService.insert(user,roles);
        return JSONResult.ok("添加成功");
    }
    @RequestMapping("/getUserUpdatePage")
    public String getUserUpdatePage() {
        return "user/user_update";
    }
    @RequestMapping("/selectUserAndRoles")
    @ResponseBody
    public JSONResult selectUserAndRoles (Integer userId) {
        Map<String,Object> map = userService.selectUserAndRoles(userId);
        return JSONResult.ok(map);
    }
    @RequestMapping("/update")
    @ResponseBody
    public JSONResult update(User user,String roles) {
        userService.update(user,roles);
        return JSONResult.ok("修改成功");
    }
    @RequestMapping("/deleteById")
    @ResponseBody
    public JSONResult deleteById(Integer userId) {
        userService.deleteById(userId);
        return JSONResult.ok("删除成功");
    }
    @RequestMapping("/deleteAll")
    @ResponseBody
    public JSONResult deleteAll(String ids) {
        String[] idArray = ids.split(",");
        for (String userId : idArray) {
            userService.deleteById(Integer.parseInt(userId));
        }
        return JSONResult.ok("删除成功");
    }
    @RequestMapping("/getUserInfoPage")
    public String getUserInfoPage() {
        return "user/user_info";
    }
    @RequestMapping("/selectUserInfo")
    @ResponseBody
    public JSONResult selectUserInfo() {
        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getPrincipal();
        return userService.selectUserInfo(username);
    }
    @RequestMapping("/getUserUpdatePasswordPage")
    public String getUserUpdatePasswordPage() {
        return "user/user_updatepassword";
    }
    @RequestMapping("/logout")
    public String logout() {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            subject.logout();
        }
        return "login";
    }
    @RequestMapping("/updatePassword")
    @ResponseBody
    public JSONResult updatePassword(String passwordFirst,String passwordSecond) {
        return userService.updatePassword(passwordFirst,passwordSecond);
    }
}

